/*
 * Copyright 2025 LinQingYing. and contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of this source code is governed by the Apache License 2.0,
 * which allows users to freely use, modify, and distribute the code,
 * provided they adhere to the terms of the license.
 *
 * The software is provided "as-is", and the authors are not responsible for
 * any damages or issues arising from its use.
 *
 */

package org.cangnova.cangjie.project.service.impl

import com.intellij.openapi.vfs.VirtualFile
import org.cangnova.cangjie.project.model.FeatureState
import org.cangnova.cangjie.project.model.PackageFeature

typealias FeatureName = String


abstract class UserDisabledFeatures {

    abstract val pkgRootToDisabledFeatures: Map<VirtualFile, Set<FeatureName>>

//    fun getDisabledFeatures(packages: Iterable<CjpmWorkspace.Package>): List<PackageFeature> {
//        return packages.flatMap { pkg ->
//            pkgRootToDisabledFeatures[pkg.rootDirectory]
//                ?.mapNotNull { name -> PackageFeature(pkg, name).takeIf { it in pkg.features } }
//                ?: emptyList()
//        }
//    }

    fun isEmpty(): Boolean {
        return pkgRootToDisabledFeatures.isEmpty() || pkgRootToDisabledFeatures.values.all { it.isEmpty() }
    }

    fun toMutable(): MutableUserDisabledFeatures = MutableUserDisabledFeatures(
        pkgRootToDisabledFeatures
            .mapValues { (_, v) -> v.toMutableSet() }
            .toMutableMap()
    )


    companion object {
        val EMPTY: UserDisabledFeatures = ImmutableUserDisabledFeatures(emptyMap())

    }
}


private class ImmutableUserDisabledFeatures(
    override val pkgRootToDisabledFeatures: Map<VirtualFile, Set<FeatureName>>
) : UserDisabledFeatures()

class MutableUserDisabledFeatures(
    override val pkgRootToDisabledFeatures: MutableMap<VirtualFile, MutableSet<FeatureName>>
) : UserDisabledFeatures() {

    fun setFeatureState(
        feature: PackageFeature,
        state: FeatureState
    ) {
        val packageRoot = feature.model.rootDir
        when (state) {
            FeatureState.Enabled -> {
                pkgRootToDisabledFeatures[packageRoot]?.remove(feature.name)
            }

            FeatureState.Disabled -> {
                pkgRootToDisabledFeatures.getOrPut(packageRoot) { hashSetOf() }
                    .add(feature.name)
            }
        }
    }
}
